﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace SalesSys.Comm.DAL
{

    public interface ISqlAddParam : ISqlTextCmd
    {
        /// <summary>
        /// 向当前命令中添加参数，并返回自己
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        ISqlAddParam AddParam(string name, object value);      
    }
    public interface ISqlTextCmd
    {
        SqlText SqlTextInfo { get; }
        IDataParameter[] GetParamArray();
    }

    /// <summary>
    /// 表示一个sql字符串命令，这是一个默认T-sql的实现；
    /// 不同数据库应重写CreateParams做不同实现
    /// </summary>
    public class SqlText : ISqlTextCmd
    {

        public static implicit operator SqlText(string sql)
        {
            return new SqlText(sql);
        }
         SqlText(string sql)
        {
            sqlStr = sql;
        }
        string sqlStr;
        public string SqlStr
        {
            get { return sqlStr; }
            private set { sqlStr = value; }
        }


        public virtual ISqlAddParam CreateParams(string name, object value)
        {
            return new SqlTextCmd(this).AddParam(name, value);
        }
        # region ISqlTextCmd
        SqlText ISqlTextCmd.SqlTextInfo
        {
            get { return this; }
        }
        IDataParameter[] ISqlTextCmd.GetParamArray()
        {
            return null;
        }
        #endregion
    }
    /// <summary>
    /// SqlParameter类型的实现
    /// </summary>
    public class SqlTextCmd : ISqlAddParam
    {
        internal SqlTextCmd(SqlText sqlText)
        {
            sqlTextInfo = sqlText;
        }
        public IDataParameter[] GetParamArray()
        {
            return paramList.ToArray();
        }
        List<IDataParameter> paramList = new List<IDataParameter>();
        public ISqlAddParam AddParam(string name, object value)
        {
            var p = new SqlParameter(name, value);
            paramList.Add(p);
            return this;
        }
         readonly SqlText sqlTextInfo;
        public SqlText SqlTextInfo
        {
            get {return sqlTextInfo; }
        }
       
    }
   

    public  class SqlTextCfg
    {
       protected string SqlType;
       protected SqlTextCfg() { LoadCfg(); }
       public override string ToString()
       {
           return SqlType;
       }

       protected virtual void LoadCfg()
       { }


         #region cfg
       // public static SqlTextList instance;
     
        protected static T LoadCfg<T>(string cfgName = null) where T:SqlTextCfg,new()
        {
            if (!string.IsNullOrWhiteSpace(cfgName))
            {
                throw new NotImplementedException("配置文件获取暂未实现");
            }
            return new T();
        }

        #endregion
    }
   



}
